<?php
namespace Article\Model;
use Think\Model;
class ArticleCategoryModel extends Model {

	public $page; //当前页
	public $perpage; //每页显示
	public $category_id; //分类ID
	public $article_id; //文章ID

	//取得适应前端框架的列表
	public function get_category_list()
	{
		//取得树状列表
		$cate_list=array();
		$i=0;
		$Model=M("ArticleCategory");
		$list=$Model->where('category_parent_id=0')->field('category_id,category_parent_id,category_name,category_show')->order('category_sort asc,category_id asc')->select();
		foreach($list as $k=>$v){
			$cate_list[$i]['category_id']=$v['category_id'];
			$cate_list[$i]['category_name']=$v['category_name'];
			$cate_list[$i]['category_parent_id']=0;
			$cate_list[$i]['category_parent_name']='顶级目录';
			$cate_list[$i]['category_show']=$v['category_show'];
			$i++;
			$list2=$Model->where('category_parent_id='.$v['category_id'])->field('category_id,category_parent_id,category_name,category_show')->order('category_sort asc,category_id asc')->select();
			foreach($list2 as $k2=>$v2){
				$cate_list[$i]['category_id']=$v2['category_id'];
				$cate_list[$i]['category_name']='-->'.$v2['category_name'];
				$cate_list[$i]['category_parent_id']=$v2['category_parent_id'];
				$cate_list[$i]['category_parent_name']=M('ArticleCategory')->where('category_id='.$v2['category_parent_id'])->getField('category_name');
				$cate_list[$i]['category_show']=$v2['category_show'];
				$i++;
			}
		}
		//将数组重新遍历，取出限制的数组
		if($this->page>0&&$this->perpage>0){
			$page=$this->page;
			$perpage=$this->perpage;
			$start=($page-1)*$perpage;
			$end=$start+$perpage;
			$category_list=array();
			foreach($cate_list as $k=>$v){
				if($k>=$start&&$k<$end){
					$category_list[$k]=$v;
				}
			}
			//dump($category_list);exit;
			return $category_list;
		}else{
			return $cate_list;
		}
	}

}